﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

namespace Square.Activity.ActivityTestWP
{
    [ToolboxItemAttribute(false)]
    public class ActivityTestWP : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Button BtnSave;
        Button BtnSet;
        TextBox txtMemo;
        SquareActivityHelper sHelper;
        RadioButtonList rblType;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            rblType = new RadioButtonList();
            rblType.RepeatDirection = RepeatDirection.Vertical;
            foreach (SquareActivities actType in Enum.GetValues(typeof(SquareActivities)))
            {
                if (!actType.ToString().Contains("All"))
                {
                    rblType.Items.Add(actType.ToString());
                }
            }
            Controls.Add(rblType);

            BtnSet = new Button();
            BtnSet.Text = "초기셋팅";
            BtnSet.Click += new EventHandler(BtnSet_Click);
            Controls.Add(BtnSet);

            BtnSave = new Button();
            BtnSave.Text = "저장";
            BtnSave.Click += new EventHandler(BtnSave_Click);
            Controls.Add(BtnSave);

            txtMemo = new TextBox();
            txtMemo.TextMode = TextBoxMode.MultiLine;
            txtMemo.Rows = 4;
            txtMemo.Text = "여기에 입력";
            Controls.Add(txtMemo);

        }

        void BtnSet_Click(object sender, EventArgs e)
        {
            sHelper = new SquareActivityHelper();
            sHelper.SetupActivities(this.Page);
        }


        void BtnSave_Click(object sender, EventArgs e)
        {
            bool bSave = false;
            string sType = string.Empty;
            SquareActivities aType = new SquareActivities();
            switch (rblType.SelectedValue)
            {
                case "SquareDocDownload":aType = SquareActivities.SquareDocDownload; break;
                case "SquareDocFolderCreate":aType = SquareActivities.SquareDocFolderCreate;break;
                case "SquareDocFolderDelete":aType = SquareActivities.SquareDocFolderDelete;break;
                case "SquareDocUpload":aType = SquareActivities.SquareDocUpload;break;
                case "SquareMemoDelete":aType = SquareActivities.SquareMemoDelete;break;
                case "SquareMemoEdit":aType = SquareActivities.SquareMemoEdit;break;
                case "SquareMemoWrite":aType = SquareActivities.SquareMemoWrite;break;
                case "SquarePeopleAccept":aType = SquareActivities.SquarePeopleAccept;break;
                case "SquarePeopleRequest":aType = SquareActivities.SquarePeopleRequest;break;
            }
            
            sHelper = new SquareActivityHelper();
            bSave = sHelper.SaveActivity(this.Page, aType, txtMemo.Text);

            if (bSave) return;
        }


        protected override void Render(HtmlTextWriter writer)
        {
            DataSet ds = null;
            string sType = string.Empty;
            if (Page.Request.Params["sType"] != null)
            {
                sType = Page.Request.Params["sType"] as string;
            }

            SquareActivities aType = new SquareActivities();
            switch (sType)
            {
                case "Memo": aType = SquareActivities.MemoAll; break;
                case "Doc": aType = SquareActivities.DocAll; break;
                case "People": aType = SquareActivities.PeopleAll; break;
            }

            if (string.IsNullOrEmpty(sType))
            {
                aType = SquareActivities.MemoAll;
            }

            sHelper = new SquareActivityHelper();
            ds = sHelper.GetActivity(this.Page, aType);

            BtnSet.RenderControl(writer);
            writer.WriteLine("</br>");
            rblType.RenderControl(writer);
            writer.WriteLine("</br>");
            txtMemo.RenderControl(writer);
            writer.WriteLine("</br>");
            BtnSave.RenderControl(writer);
            writer.WriteLine("</br></br>");
            writer.WriteLine("<table border=\"1\" cellpadding=\"5\" cellspacing=\"2\">");
            writer.WriteLine("    <tr>");
            writer.WriteLine("        <td>사진</td><td>Publisher</td><td>사진</td><td>Owner</td><td>내용</td><td>날짜</td><td>시간</td><td>타입</td><td>기타</td>");
            writer.WriteLine("    </tr>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                writer.WriteLine("    <tr>");
                writer.WriteLine("        <td><img src=\"" + dr["PublisherPic"] + "\" alt=\"" + dr["Publisher"] + "\"></img></td>");
                writer.WriteLine("        <td><a href=\"" + dr["PublisherLink"] + "\">" + dr["Publisher"] + "</a></td>");
                writer.WriteLine("        <td><img src=\"" + dr["OwnerPic"] + "\" alt=\"" + dr["Owner"] + "\"></img></td>");
                writer.WriteLine("        <td><a href=\"" + dr["OwnerLink"] + "\">" + dr["Owner"] + "</a></td>");
                writer.WriteLine("        <td>" + dr["Value"] + "</td>");
                writer.WriteLine("        <td>" + dr["ActivityDate"] + "</td>");
                writer.WriteLine("        <td>" + dr["ActivityTime"] + "</td>");
                writer.WriteLine("        <td>" + dr["ActivityType"] + "</td>");
                writer.WriteLine("        <td>" + dr["Reserved"] + "</td>");
                writer.WriteLine("    </tr>");
            }

            writer.WriteLine("</table>");

        }
    }
}
